package cn.dgut.changeManager;

import cn.dgut.Entity.Course;
import cn.dgut.Entity.Student;
import cn.dgut.Entity.Teacher;
import cn.dgut.Utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/DeleteList")
public class DeleteListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int page = Integer.parseInt(request.getParameter("page"));
        String id = request.getParameter("id");
        JDBCUtils jdbcUtils = new JDBCUtils();
        List<String> sqlList = new ArrayList<>();
        if(page==0){
            sqlList.add("delete from User where user_id = "+id);
        }else if(page==1){
            sqlList.add("delete from Student where student_id = "+id);
            Student stu = (Student) jdbcUtils.queryOneData("Student", Integer.parseInt(id));
            sqlList.add("delete from Select_Course where sid = "+stu.getStudent_id());
        }else if(page==2){
            sqlList.add("delete from Teacher where teacher_id = "+id);
            Teacher teacher = (Teacher) jdbcUtils.queryOneData("Teacher", Integer.parseInt(id));
            sqlList.add("delete from Course where tid = "+teacher.getTeacher_id());
        }else if(page==3){
            sqlList.add("delete from Course where course_id = "+id);
            Course course = (Course) jdbcUtils.queryOneData("Course", Integer.parseInt(id));
            sqlList.add("delete from Select_Course where course_id = "+course.getCourse_id());
        }else if(page==4){
            sqlList.add("delete from Select_Course where id = "+id);
        }else if(page==5){
            sqlList.add("delete from StudentWorks where StudentWorks_id = "+id);
        }else if(page==6){
            sqlList.add("delete from Recruit where recruit_id = "+id);
        }else if(page==7){
            sqlList.add("delete from Teacher where teacher_id = "+id);
        }else {
            return;
        }

        for (int i = 0; i < sqlList.size(); i++) {
            jdbcUtils.querySQL(sqlList.get(i));
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("isRight", 1);
        response.sendRedirect("/backstage/main_page.jsp?page="+page);



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}